package com.yanqu.road.entity;

import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIgnore;

public class DataObject {
    @JsonIgnore
    private short op = 0;
    @JsonIgnore
    private long beginWriteTime = 0;
    @JsonIgnore
    private long updateOptionTime;
    @JsonIgnore
    private boolean needInsert;

    @JSONField(deserialize = false, serialize = false)
    public boolean isNeedInsert() {
        return needInsert;
    }

    @JSONField(deserialize = false, serialize = false)
    public void setNeedInsert(boolean needInsert) {

        this.needInsert = needInsert;
    }

    @JSONField(deserialize = false, serialize = false)
    public synchronized void setOp(short option) {
        if (option == DataStatus.Update) {
            //记录更新时间
            this.updateOptionTime = System.currentTimeMillis();
        }
        if ((this.op == DataStatus.Insert) && (option == DataStatus.Update)) {
            //是插入状态，不允许更改为更新状态
            return;
        }
        if (option == DataStatus.None) {//设置为无状态
            if (beginWriteTime != 0 && updateOptionTime >= beginWriteTime) {
                //如果写入时间不等于0 且 更新时间大于准备写入时间，则说明需要重新更新，不允许设置为无状态
                this.op = DataStatus.Update;
                return;
            }
        }
        this.op = option;
    }

    @JSONField(deserialize = false, serialize = false)
    public final short getOp() {
        return this.op;
    }

    @JSONField(deserialize = false, serialize = false)
    public boolean isDirty() {
        if (op == DataStatus.Insert || op == DataStatus.Update)
            return true;
        else
            return false;
    }

    @JSONField(deserialize = false, serialize = false)
    public boolean isUpdateOption() {
        return DataStatus.Update == op;
    }

    @JSONField(deserialize = false, serialize = false)
    public boolean isInsertOption() {
        return DataStatus.Insert == op;
    }

    @JSONField(deserialize = false, serialize = false)
    public void setUpdateOption() {
        setOp(DataStatus.Update);
    }

    @JSONField(deserialize = false, serialize = false)
    public void setNoneOption() {
        setOp(DataStatus.None);
    }

    @JSONField(deserialize = false, serialize = false)
    public void setInsertOption() {
        setOp(DataStatus.Insert);
    }

    @JSONField(deserialize = false, serialize = false)
    public void setBeginWriteTime() {
        this.beginWriteTime = System.currentTimeMillis();
    }

    @JSONField(deserialize = false, serialize = false)
    public long getBeginWriteTime() {
        return beginWriteTime;
    }

    @JSONField(deserialize = false, serialize = false)
    public void setBeginWriteTime(long beginWriteTime) {
        this.beginWriteTime = beginWriteTime;
    }

    @JSONField(deserialize = false, serialize = false)
    public long getUpdateOptionTime() {
        return updateOptionTime;
    }

    @JSONField(deserialize = false, serialize = false)
    public void setUpdateOptionTime(long updateOptionTime) {
        this.updateOptionTime = updateOptionTime;
    }
}
